package com.ourteam.workspace.api;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.lang.SystemUtils;

import com.ourteam.scm.utils.ISCMService;
import com.ourteam.scm.utils.VersionInfo;
import com.ourteam.workspace.dao.WorkspaceResourceDAOQueryBean;
import com.ourteam.workspace.domain.WorkspaceBean;
import com.ourteam.workspace.domain.WorkspaceResourceBean;

public class WorkspaceSubFileTreeNodeBuilder extends AbstractWorkspaceResourceTreeNodeBuilder<WorkspaceBean> {

	@Override
	public WorkspaceResourceFileBean[] getTreeNodeObjects(WorkspaceBean parentObject) throws Exception {

		final File workSpacePath = new File(getWorkspaceRootDir(), parentObject.getWorkspacePath());

		if (workSpacePath.exists() == false) {
			return new WorkspaceResourceFileBean[0];
		}

		WorkspaceResourceDAOQueryBean queryBean = new WorkspaceResourceDAOQueryBean();

		queryBean.createCriteria().andWorkspaceIdEqualTo(parentObject.getId());

		WorkspaceResourceBean[] resourceBeans = workspaceService.queryWorkspaceResources(queryBean);

		final List<String> resourcePaths = new ArrayList<String>();

		for (int i = 0; i < resourceBeans.length; i++) {
			WorkspaceResourceBean workspaceResourceBean = resourceBeans[i];
			resourcePaths.add(
					workSpacePath.getPath() + SystemUtils.FILE_SEPARATOR + workspaceResourceBean.getResourcePath());
		}

		Collection<File> files = FileUtils.listFilesAndDirs(workSpacePath, new IOFileFilter() {

			@Override
			public boolean accept(File arg0, String arg1) {
				return arg0.getParentFile().equals(workSpacePath) && arg0.isHidden() == false;
			}

			@Override
			public boolean accept(File arg0) {
				return arg0.getParentFile().equals(workSpacePath) && arg0.isHidden() == false;
			}
		}, new IOFileFilter() {

			@Override
			public boolean accept(File arg0, String arg1) {
				return arg0.getParentFile().equals(workSpacePath) && arg0.isHidden() == false
						&& resourcePaths.contains(arg0.getPath()) == false;
			}

			@Override
			public boolean accept(File arg0) {
				return arg0.getParentFile().equals(workSpacePath) && arg0.isHidden() == false
						&& resourcePaths.contains(arg0.getPath()) == false;
			}
		});

		files.remove(workSpacePath);

		File[] fileArray = FileUtils.convertFileCollectionToFileArray(files);

		Arrays.sort(fileArray, new Comparator<File>() {

			@Override
			public int compare(File o1, File o2) {
				if (o1.isDirectory() && o2.isFile()) {
					return -1;
				} else if (o1.isFile() && o2.isDirectory()) {
					return 1;
				} else {
					return 0;
				}

			}

		});

		WorkspaceResourceFileBean[] resourceFileBeans = new WorkspaceResourceFileBean[fileArray.length];

		ISCMService iscmService = WorkspaceUtils.getSCMService(parentObject.getId());

		Map<String, VersionInfo> versionMap = iscmService.getResourceVersionInfos(getWorkspaceRootDir(), fileArray);

		for (int i = 0; i < resourceFileBeans.length; i++) {
			resourceFileBeans[i] = new WorkspaceResourceFileBean(parentObject, fileArray[i]);
			if (versionMap.containsKey(fileArray[i].getName())) {
				resourceFileBeans[i].setVersionInfo(versionMap.get(fileArray[i].getName()));
			}
		}

		return resourceFileBeans;
	}

	@Override
	public WorkspaceBean converToParentObject(Object parent) throws Exception {
		return workspaceService.getWorkspaceById(Long.parseLong(parent.toString()));
	}

}
